package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.Toast;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.RequisitionDetail;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.widget.MultiLinesViewNew;
import com.isunland.managesystem.widget.SingleLineViewNew;
import java.util.HashMap;

/**
 * Created by wu on 2016/4/8.
 * 调休关联的差旅单详情
 */
public class CheckedOffTripDetailFragment extends BaseFragment {

  public static final String EXTRA_ID =
      "com.isunland.managesystem.ui.CheckedOffTripDetailFragment.EXTRA_ITEM";
  private String mId;
  private MultiLinesViewNew mEtReason;
  private SingleLineViewNew mTvActualStartTime;
  private SingleLineViewNew mTvActualEndTime;
  private SingleLineViewNew mEtNautralDays;

  private RadioButton mRbApplyTrip; //申请
  private RadioButton mRbSupplyTrip;//补填

  private String mIfLaterWriter = "";//是申请还是补填
  private BaseVolleyActivity mActivity;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitleCustom(R.string.tripDetail);
    mId = getArguments().getString(EXTRA_ID);
    if ((mId != null) && (mId.contains("'"))) {
      mId = mId.replaceAll("'", "");
    }
    mActivity = (BaseVolleyActivity) getActivity();
    volleypostInit();
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_check_trip, container, false);
    mEtReason = (MultiLinesViewNew) v.findViewById(R.id.et_overTimeReason_checkOver);
    mTvActualStartTime = (SingleLineViewNew) v.findViewById(R.id.tv_actualBeginTime_checkOver);
    mTvActualEndTime = (SingleLineViewNew) v.findViewById(R.id.tv_actualEndTime_checkOver);
    mEtNautralDays = (SingleLineViewNew) v.findViewById(R.id.et_naturalTripDays_checkTrip);

    //RadioGroup mRgTripType = (RadioGroup) v.findViewById(R.id.rg_type);
    mRbApplyTrip = (RadioButton) v.findViewById(R.id.rb_apply);
    mRbSupplyTrip = (RadioButton) v.findViewById(R.id.rb_supply);

    //TextView mtvStartTimeTrip = (TextView) v.findViewById(R.id.tv_startTime_trip);
    //TextView mtvEndTimeTrip = (TextView) v.findViewById(R.id.tv_endTime_trip);
    initData();
    return v;
  }

  private void volleypostInit() {
    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_REQUISITION_DETAIL);
    HashMap<String, String> params = new HashMap<>();
    params.put("attendType", "trip");
    params.put("id", mId);
    MyUtils.showDialogLoading(getActivity());
    mActivity.volleyPost(url, params, new VolleyResponse() {

      @Override public void onVolleyResponse(String response) {

        MyUtils.dismissDialog();
        try {
          RequisitionDetail requisitionDetail =
              new Gson().fromJson(response, RequisitionDetail.class);

          mEtReason.getTvContent().setText(requisitionDetail.getBtripReason());// 3.获取差旅原因.
          mTvActualStartTime.getTvContent().setText(requisitionDetail.getActSTime());
          mTvActualEndTime.getTvContent().setText(requisitionDetail.getActETime());
          mIfLaterWriter = requisitionDetail.getBtripType();
          if ("1".equalsIgnoreCase(mIfLaterWriter)) {
            mRbSupplyTrip.setChecked(true);
          }
          if ("0".equalsIgnoreCase(mIfLaterWriter)) {
            mRbApplyTrip.setChecked(true);
          }

          mEtNautralDays.getTvContent().setText(requisitionDetail.getNaturalDays());// 5.自然天数..

          mEtReason.getTvContent().setEnabled(false);
          mRbApplyTrip.setEnabled(false);
          mRbSupplyTrip.setEnabled(false);
        } catch (JsonSyntaxException e) {
          LogUtil.e("error", e);

          Toast.makeText(getActivity(), R.string.wrong_data, Toast.LENGTH_SHORT).show();
        }
      }

      @Override public void onVolleyError(VolleyError arg0) {
        MyUtils.dismissDialog();
        Toast.makeText(getActivity(), R.string.noConnectionToServer, Toast.LENGTH_SHORT).show();
      }
    });
  }

  public static Fragment newInstance(String id) {
    Bundle bundle = new Bundle();
    bundle.putString(EXTRA_ID, id);
    CheckedOffTripDetailFragment fragment = new CheckedOffTripDetailFragment();
    fragment.setArguments(bundle);
    return fragment;
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回图标
      case android.R.id.home:
        //
        //                if (NavUtils.getParentActivityName(getActivity()) != null) {
        //                    NavUtils.navigateUpFromSameTask(getActivity());
        //                }
        getActivity().finish();
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }
}
